﻿using pcDAL;
using pcObjectLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace pcBLL
{
    public class UserBLL
    {

        public int Add(UserObjLibrary user)
        {
            UserDAL ud = new UserDAL();
            PasswordBLL pb = new PasswordBLL();
            user.passwordSalt = PasswordBLL.GenerateSalt();
            user.passwordHash = PasswordBLL.EncodePassword(user.password, user.passwordSalt);
            return ud.Add(user);
        }

        public static bool Login(string userName, string password, string ipAddress, string MacAddress)
        {
            return PasswordBLL.ValidatePassword(userName: userName, Password: password, ipAddress: ipAddress, MacAddress: MacAddress) > 0;
        }

        public static bool ChangePassword(string userName, string oldPassword, string newPassword, string macAddress, string ipAddress)
        {
            bool updated = false;
            if (Login(userName: userName, password: oldPassword, ipAddress: ipAddress,MacAddress: macAddress))
            {
                UpdatePassword(userName, newPassword, macAddress, ipAddress);
                updated = true;
            }
            return updated;
        }

        private static void UpdatePassword(string userName, string newPassword, string macAddress, string ipAddress)
        {
            UserDAL ud = new UserDAL();
            var user = ud.Details(userName);
            user.passwordHash = PasswordBLL.EncodePassword(newPassword, user.passwordSalt);
            user.lastActive_MAC_address = macAddress;
            user.lastActivity_ip = ipAddress;
            ud.UpdatePassword(user);
        }

        public UserObjLibrary Details(string userName)
        {
            UserDAL ud = new UserDAL();
            return ud.Details(userName);
        }

        public UserObjLibrary Details(int userId)
        {
            UserDAL ud = new UserDAL();
            return ud.Details(userId: userId);
        }
        public int Update(UserObjLibrary user, bool adminUpdate)
        {
            UserDAL ud = new UserDAL();
            return ud.Update(user, adminUpdate);
        }

        public string getTimeZoneOfUser(int userId)
        {
            UserDAL ud = new UserDAL();
            return ud.getTimeZoneOfUser(userId: userId);
        }

        public List<SelectListItem> GetAllCompanyEmployees(int companyId, int opUserTypeId)
        {
            UserDAL ud = new UserDAL();
            return ud.GetAllCompanyEmployees(companyId: companyId, opUserTypeId: opUserTypeId);
        }



        public List<HolidaysObjLibrary> GetCompanyHolidays(int companyId, int userId, DateTime stDate, DateTime enDate)
        {
            UserDAL ud = new UserDAL();
            List<HolidaysObjLibrary>  holidays =  ud.GetCompanyHolidays(companyId: companyId, userId: userId, stDate: stDate, enDate: enDate);
            if (stDate != DateTime.MinValue)
                holidays = holidays.Where(x => x.HolidayDate >= stDate.Date).ToList();
            if (enDate != DateTime.MinValue)
                holidays = holidays.Where(x => x.HolidayDate <= enDate.Date).ToList();
            return holidays;
        }

        public List<UserObjLibrary> GetAllUsers(int companyId)
        {
            UserDAL ud = new UserDAL();
            return ud.GetAllUsers(companyId: companyId);
        }
    }
}
